<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="身份证号" prop="id">
      <el-input v-model="dataForm.id" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="人员类型" prop="classification">
      <el-select v-model="dataForm.classification" placeholder="人员类型">
        <el-option key="0" label="助手" value="0"></el-option>
        <el-option key="1" label="司机" value="1"></el-option>
      </el-select>
<!--      <el-input v-model="dataForm.classification" placeholder="人员类型（司机1，助手0）"></el-input>-->
    </el-form-item>
    <el-form-item label="手机号" prop="iphone">
      <el-input v-model="dataForm.iphone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="所属车商" prop="cardealer">
      <el-input v-model="dataForm.cardealer" placeholder="所属车商"></el-input>
    </el-form-item>
    <el-form-item label="车辆识别代号" prop="vehiclenumber">
      <el-input v-model="dataForm.vehiclenumber" placeholder="车辆识别代号"></el-input>
    </el-form-item>
    <el-form-item label="驾驶证号" prop="drivernumber">
      <el-input v-model="dataForm.drivernumber" placeholder="驾驶证号码"></el-input>
    </el-form-item>
    <el-form-item label="排放标准" prop="emissionstandard">
      <el-input v-model="dataForm.emissionstandard" placeholder="排放标准"></el-input>
    </el-form-item>
    <el-form-item label="注册日期" prop="datetime">
      <el-input v-model="dataForm.datetime" placeholder="注册日期"></el-input>
    </el-form-item>
    <el-form-item label="考试有效期" prop="datetimevli">
      <el-input v-model="dataForm.datetimevli" placeholder="有效期"></el-input>
    </el-form-item>
    <el-form-item label="头像照片" prop="picture1">
      <el-input v-model="dataForm.picture1" placeholder="头像照片目录以及文件名"></el-input>
    </el-form-item>
    <el-form-item label="身份证照" prop="picture2">
      <el-input v-model="dataForm.picture2" placeholder="身份证照片目录以及文件名"></el-input>
    </el-form-item>
    <el-form-item label="驾驶证照" prop="picture3">
      <el-input v-model="dataForm.picture3" placeholder="驾驶证照片目录以及文件名"></el-input>
    </el-form-item>
    <el-form-item label="车辆行驶" prop="picture4">
      <el-input v-model="dataForm.picture4" placeholder="车辆行驶照片目录以及文件名"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          id: '',
          name: '',
          classification: '',
          iphone: '',
          cardealer: '',
          vehiclenumber: '',
          drivernumber: '',
          emissionstandard: '',
          datetime: '',
          datetimevli: '',
          picture1: '',
          picture2: '',
          picture3: '',
          picture4: ''
        },
        dataRule: {
          id: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          classification: [
            { required: true, message: '人员类型不能为空', trigger: 'blur' }
          ],
          iphone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          cardealer: [
            { required: true, message: '所属车商不能为空', trigger: 'blur' }
          ],
          vehiclenumber: [
            { required: true, message: '车辆识别代号不能为空', trigger: 'blur' }
          ],
          drivernumber: [
            { required: true, message: '驾驶证号码不能为空', trigger: 'blur' }
          ],
          emissionstandard: [
            { required: true, message: '排放标准不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/driversdatcher/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.driversDatcher.id
                this.dataForm.name = data.driversDatcher.name
                this.dataForm.classification = data.driversDatcher.classification
                this.dataForm.iphone = data.driversDatcher.iphone
                this.dataForm.cardealer = data.driversDatcher.cardealer
                this.dataForm.vehiclenumber = data.driversDatcher.vehiclenumber
                this.dataForm.drivernumber = data.driversDatcher.drivernumber
                this.dataForm.emissionstandard = data.driversDatcher.emissionstandard
                this.dataForm.datetime = data.driversDatcher.datetime
                this.dataForm.datetimevli = data.driversDatcher.datetimevli
                this.dataForm.picture1 = data.driversDatcher.picture1
                this.dataForm.picture2 = data.driversDatcher.picture2
                this.dataForm.picture3 = data.driversDatcher.picture3
                this.dataForm.picture4 = data.driversDatcher.picture4
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/driversdatcher/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'id': this.dataForm.id,
                'name': this.dataForm.name,
                'classification': this.dataForm.classification,
                'iphone': this.dataForm.iphone,
                'cardealer': this.dataForm.cardealer,
                'vehiclenumber': this.dataForm.vehiclenumber,
                'drivernumber': this.dataForm.drivernumber,
                'emissionstandard': this.dataForm.emissionstandard,
                'datetime': this.dataForm.datetime,
                'datetimevli': this.dataForm.datetimevli,
                'picture1': this.dataForm.picture1,
                'picture2': this.dataForm.picture2,
                'picture3': this.dataForm.picture3,
                'picture4': this.dataForm.picture4
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
